/*
 * DisplayResults.java
 *
 * Created on March 14, 2005, 7:49 PM
 * 05, Sep; 2007 class name changed from DisplayResults to DisplayResults
 */

package portfolio.stock.analize;
import portfolio.stock.ConfigureSVMPanel;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Vector;
import java.text.DecimalFormat;
import portfolio.mainScreen;
import preprocess.*;

/**
 *
 * @author  ACA
 */
public class DisplayResults extends JScrollPane {
    private mainScreen     mainFrame;
    //JScrollPane            scrollResults = null;
    private JPanel         jDisplayPanel = null;
    private JTextArea      jta;
    private String         sStockName;
    private int            iTerm;
    private Vector         vTimeLine;
    private double [][]    dDataIn;
    private Vector         vTableHeader;
    private ConfigureSVMPanel configurePanel;
    private Preprocess     preprocess;
    private String         sImageName;
    
    private int            iIndex;
    private int            iDataIdx;
    private int            iDataEnd;
    
    private double         dInitialPrice;
    private double         dPredictedPrice;
    private double         dReturn;
    
    private String         sBeginDate;
    private String         sBeginTrain;
    private String         sEndTrain;
    private String         sBeginTerm;
    private String         sEndTerm;

    private boolean        bPreprocessCheck = false;
    private String         sPreprocessGroup;
    private int            iComponentsConfig;
    private double [][]    dEigenValues;
    private String         sTypeSVMGroup;
    private String         sSVMKernelGroup;
    
    /**
     * Creates a new instance of DisplayResults
     * @param frameParam 
     * @param sNameParam 
     * @param iTermParam 
     * @param tLineParam 
     * @param dataParam 
     * @param vHeaderParam 
     * @param configParam 
     * @param preproParam 
     */
    public DisplayResults(mainScreen       frameParam, 
                         String            sNameParam, 
                         int               iTermParam, 
                         Vector            tLineParam, 
                         double [][]       dataParam,
                         Vector            vHeaderParam,
                         ConfigureSVMPanel configParam,
                         Preprocess        preproParam
                         
                         ) {
                             
        super (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
               JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        jDisplayPanel = frameParam.getResultsPanel();
        jDisplayPanel.setLayout(new BorderLayout ());
        jDisplayPanel.setBorder (BorderFactory.createTitledBorder(new EtchedBorder (Color.WHITE, Color.GRAY )));
        
        configurePanel = configParam;
        preprocess     = preproParam;
        vTableHeader   = vHeaderParam;
               
        jta = new JTextArea ();
        jta.setWrapStyleWord (true);
        jta.setWrapStyleWord (true);
        jta.setEditable (false);
        //scrollResults = new JScrollPane (jta);
        this.setViewportView (jta);

        DecimalFormat dFormat = new DecimalFormat ("#0.00");
        
        mainFrame    = frameParam;
        sStockName   = sNameParam;
        iTerm        = iTermParam;
        vTimeLine    = tLineParam;
        dDataIn      = dataParam;
        iIndex       = (iTerm != 0)? iTerm -1 : 0;
        iDataIdx     = (dDataIn.length - iIndex  >= 0)? dDataIn.length -iIndex : dDataIn.length -1;
        iDataEnd     = (dDataIn.length -1        >= 0)? dDataIn.length -1        : 0;
        
        dInitialPrice   = dDataIn [iDataIdx][1];
        dPredictedPrice = dDataIn [iDataEnd][1];
        
        dReturn = (dInitialPrice > 0)?
                         (dPredictedPrice - dInitialPrice) /
                          dInitialPrice : 0;
                          
        sBeginDate  = vTimeLine.get(0).toString();
        sBeginTrain = vTimeLine.get(iIndex).toString();
        sEndTrain   = vTimeLine.get(dDataIn.length - 2 * iIndex).toString();
        sBeginTerm  = vTimeLine.get(dDataIn.length - iIndex).toString();
        sEndTerm    = vTimeLine.get(dDataIn.length -1).toString();

        bPreprocessCheck = configurePanel.getPreprocessOK().isSelected(); //.getPreprocessCheck ();
        if (bPreprocessCheck == true) {
            try {
                sPreprocessGroup  = configurePanel.getPreprocessGroup ();
                iComponentsConfig = configurePanel.getComponentsConfig ();
                dEigenValues      = preprocess.getEigenValues ();
            } catch (Exception e) {
                JOptionPane.showMessageDialog (null, e);
            }
        }
        sTypeSVMGroup = configurePanel.getTypeGroupString ();
        sSVMKernelGroup = configurePanel.getKernelGroupString ();
        
        jta.append ("Stock name:\t\t" + sStockName + "\n");
        jta.append ("Term:\t\t" + iTerm +" records\n");
        jta.append ("Initial price:\t\t$"   + dFormat.format (dInitialPrice) + "\n");
        jta.append ("Predicted price:\t\t$" + dFormat.format (dPredictedPrice) + "\n");
        jta.append ("Predicted return:\t"   + dFormat.format (dReturn * 100) +"%\n\n");
        
        jta.append ("\nTime series significant dates:\n");
        jta.append ("\tBegin date:\t\t"        + sBeginDate + "\n");
        jta.append ("\tBegin training date:\t" + sBeginTrain + "\n");
        jta.append ("\tEnd training date:\t"   + sEndTrain + "\n");
        jta.append ("\tBegin term period:\t"   + sBeginTerm + "\n");
        jta.append ("\tEnd term period:\t"     + sEndTerm + "\n");
        jta.append ("\nSelected indexes:\n");
        this.getSelectedIndexes (jta);
            
        jta.append ("\nRegression configuration:\n");            
        if (bPreprocessCheck == false) {
            jta.append ("\tNo preprocess selected.\n");
        }
        else {
            jta.append ("\t"+ sPreprocessGroup + " preprocess selected.\n");
            jta.append ("\tNumber of components selected: " + iComponentsConfig +"\n");
            jta.append ("\nEigenvalue diagonal:\n");
            this.appendTextDiagonal (jta, dEigenValues);
            jta.append ("\nPCA K matrix:\n");
            this.appendTextMatrix (jta, preprocess.getKMatrix());
            if (sPreprocessGroup == "ICA") {
                jta.append ("\nICA W matrix:\n");
                this.appendTextMatrix (jta, preprocess.getWMatrix());
            }
            jta.append ("\nCorrelation matrix: \n");
            jta.append ("\nTemporarily disabled; remember to reinsert \n");
//            this.appendLebeledTextMatrix (jta, vTableHeader, configurePanel.getCorrelationMtx());
        }
            
        jta.append ("\nSMV configuration:\n");
        jta.append ("\tType:\t" + sTypeSVMGroup + "\n");
        jta.append ("\tKernel:\t" + sSVMKernelGroup + "\n");

        jDisplayPanel.add(this);
    }
    /*************************************************************************************/
    private void appendTextMatrix (JTextArea textAreaParam, double [][] dMtxParam)
    {
        if (dMtxParam == null)
            return;
        if (textAreaParam == null)
            return;
        
        int iRowNum = dMtxParam.length;
        int iColNum = dMtxParam [0].length;
        DecimalFormat dFormat = new DecimalFormat ("#00.0000");
                
        for (int i = 0; i < iRowNum; i++) {
            textAreaParam.append ("\t");
            for (int j = 0; j < iColNum; j++) {
                textAreaParam.append (dFormat.format (dMtxParam [i][j])+"\t");
            }
            textAreaParam.append ("\n");
        }
        return;
    } /* end method appendTextArea */
    /*************************************************************************************/
    private void appendLebeledTextMatrix (JTextArea textAreaParam, Vector vLabel, double [][] dMtxParam)
    {
        if (dMtxParam == null)
            return;
        if (textAreaParam == null)
            return;
        
        int iRowNum = dMtxParam.length;
        int iColNum = dMtxParam [0].length;
        DecimalFormat dFormat = new DecimalFormat ("#00.0000");
                
        for (int i = 0; i < iRowNum; i++) {
            textAreaParam.append (vLabel.get (i).toString());
            textAreaParam.append ("\t");
            for (int j = 0; j < iColNum; j++) {
                textAreaParam.append (dFormat.format (dMtxParam [i][j])+"\t");
            }
            textAreaParam.append ("\n");
        }
        return;
    } /* end method appendTextArea */    
    /*************************************************************************************/
    private void appendTextDiagonal (JTextArea textAreaParam, double [][] dMtxParam)
    {
        if (dMtxParam == null)
            return;
        if (textAreaParam == null)
            return;
        
        int iRowNum = dMtxParam.length;
        int iColNum = dMtxParam [0].length;
        if (iRowNum != iColNum)
            return;
        
        DecimalFormat dFormat = new DecimalFormat ("#00.0000");
                
        textAreaParam.append ("\t");
        for (int i = 0; i < iColNum; i++) {
            textAreaParam.append (dFormat.format (dMtxParam [i][i])+"\t");
        }
        textAreaParam.append ("\n");
            
        return;
    } /* end method appendTextArea */    
    /*************************************************************************************/
    private void getSelectedIndexes (JTextArea textAreaParam)
    {
        if (textAreaParam == null)
            return;
        int iHeaderSize = vTableHeader.size ();
        if (iHeaderSize == 0)
            return;
        
        for (int i = 0; i < iHeaderSize; i++) { 
             textAreaParam.append ("\t" + vTableHeader.get(i).toString() + "\n");
        }        
    } /* end method getSelectedIndexes */
    /*************************************************************************************/
    private void getSelectedIndexesHTML (JTextArea textAreaParam)
    {
        if (textAreaParam == null)
            return;
        int iHeaderSize = vTableHeader.size ();
        if (iHeaderSize == 0)
            return;
        
        textAreaParam.append ("<pre>");
        for (int i = 0; i < iHeaderSize; i++) { 
             textAreaParam.append ("\t" + vTableHeader.get(i).toString() + "<br>");
        }
        textAreaParam.append ("</pre>");
    } /* end method getSelectedIndexes */    
    /***************************************************************************************/
    public JTextArea getTextAreaHandle ()
    {
        JTextArea jFile = new JTextArea ();
        
        DecimalFormat dFormat = new DecimalFormat ("#0.00");
        
        jFile.setWrapStyleWord (true);
        jFile.setWrapStyleWord (true);
        jFile.setEditable (false);
        
        jFile.append ("<html>");
        jFile.append ("<p><pre>");
        jFile.append ("<b>Stock name:</b>\t\t"       + sStockName + "<br>");
        jFile.append ("<b>Term:</b>\t\t\t"           + iTerm +" records<br>");
        jFile.append ("<b>Initial price:</b>\t\t$"   + dFormat.format (dInitialPrice) + "<br>");
        jFile.append ("<b>Predicted price:</b>\t$"   + dFormat.format (dPredictedPrice) + "<br>");
        jFile.append ("<b>Predicted return:</b>\t"   + dFormat.format (dReturn * 100) +"%<br>");
        jFile.append ("</pre></p>");

        jFile.append ("<p><pre>");
        jFile.append ("<br><b>Time series significant dates:</b><br>");
        jFile.append ("\tBegin date:\t\t"        + sBeginDate + "<br>");
        jFile.append ("\tBegin training date:\t" + sBeginTrain + "<br>");
        jFile.append ("\tEnd training date:\t"   + sEndTrain + "<br>");
        jFile.append ("\tBegin term period:\t"   + sBeginTerm + "<br>");
        jFile.append ("\tEnd term period:\t"     + sEndTerm + "<br>");            
        jFile.append ("</pre></p>");

        jFile.append ("<p>");
        jFile.append ("<b>Selected indexes:</b><br>");
        this.getSelectedIndexesHTML (jFile);
        jFile.append ("</p>");

        jFile.append ("<p><pre>");
        jFile.append ("<b>Regression configuration:</b><br>");
        if (bPreprocessCheck == false) {
            jFile.append ("<b>\tNo preprocess selected.</b><br>");
        }
        else {
            jFile.append ("\t"+ sPreprocessGroup + " preprocess selected.<br>");
            jFile.append ("\tNumber of components selected: " + iComponentsConfig +"<br>");
            jFile.append ("<br><b>Eigenvalue diagonal:</b><br>");
            this.appendTextDiagonal (jFile, dEigenValues);
            jFile.append ("\n<b>PCA K matrix:</b>\n");
            this.appendTextMatrix (jFile, preprocess.getKMatrix());
            if (sPreprocessGroup == "ICA") {
                jFile.append ("\n<b>ICA W matrix:</b>\n");
                this.appendTextMatrix (jFile, preprocess.getWMatrix());
            }
            jFile.append ("<b>\nCorrelation matrix: </b>\n");
 //0j0           this.appendLebeledTextMatrix (jFile, vTableHeader, configurePanel.getCorrelationMtx());
        }
        jFile.append ("</pre></p>");

        jFile.append ("<p>");
        jFile.append ("<b>SMV configuration:</b><br>");
        jFile.append ("\tType:\t" + sTypeSVMGroup + "<br>");
        jFile.append ("\tKernel:\t" + sSVMKernelGroup + "\n");
        jFile.append ("</p>");

        jFile.append ("<p>");
        jFile.append ("<img src="+ sImageName +".jpg"+">");
        jFile.append ("</p>");
        jFile.append ("</html>");
         
        return jFile;
    } /* end method getTextHandle */
    /****************************************************************************************/
    public void setImageName (String sNameParam)
    {
        sImageName = sNameParam;
        return;
    }
} /* end class DisplayResults */
